package com.unionwo.service;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.unionwo.dao.UnionVersionDao;
import com.unionwo.model.UnionResponse;
import com.unionwo.model.UpdateCheckResponse;
@Service("checkUpdateService")
public class CheckUpdateService implements UnionService{
	@Autowired
	UnionVersionDao unionVersionDao;
	
	private Logger log = Logger.getLogger(CheckUpdateService.class);
	@Override
	public UnionResponse doRequest(HttpServletRequest request) throws Exception {
		// TODO Auto-generated method stub
		String clientVersion = request.getParameter("v");
		double d_clientVersion  = 0;
		try {
			d_clientVersion  = Double.parseDouble(clientVersion);
		} catch (Exception e1) {
			throw new IllegalArgumentException("clientVersion:"+clientVersion);
		}
		Map<String,Object> versionMap = unionVersionDao.selectVersion();
		double version = 0;
		try {
			version = Double.parseDouble(String.valueOf(versionMap.get("version")));
		} catch (Exception e) {
			log.error(e);
		}
		UpdateCheckResponse res = new UpdateCheckResponse();
		if(d_clientVersion<version){
			//need update
			res.setS(1);
			res.setUrl(String.valueOf(versionMap.get("url")));
		}else{
			//not need
			res.setS(0);
		}
		
		return res;
	}

}
